<?php

//require_once $_SERVER[HTTP_HOST].'simplewind/Core/Library/Org/Qiniu/autoload.php';

/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2017/8/24
 * Time: 10:26
 */

namespace Wechat\Controller;

// 引入鉴权类
//use Qiniu\Auth;
// 引入上传类
//use Qiniu\Storage\UploadManager;

/*转介控制器*/
class ZhuanController extends CommonController
{
    /*发布转介*/
    public function index()
    {
        /*微信自定义分享*/
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        (new WechatController())->zidingfenxiang($url);
        if(IS_POST){
            $post = i('post.');
            /*
            echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
            echo '<pre/>';
            var_dump($post);
            die();
            */

            if(trim(empty($post['type']))) {
                $this->error('请选择售组类型');
            }
            if(trim(empty($post['buildtype']))) {
                $this->error('请选择售租种类');
            }
            if(trim(empty($post['buildname']))) {
                $this->error('请输入小区名称');
            }
            if(trim(empty($post['des']))) {
                $this->error('请输入详细描述');
            }
            if(trim(empty($post['yuezu']))) {
                $this->error('请输入月租');
            }
            if(trim(empty($post['dizhi']))) {
                $this->error('请输入小区地址');
            }
            if(trim(empty($post['name']))) {
                $this->error('请输入发布人姓名');
            }
            if(trim(empty($post['phone']))) {
                $this->error('请输入发布人手机号');
            }
            if(trim(empty($post['code']))) {
                $this->error('请输入验证码');
            }
            if(!preg_match("/^1[34578]{1}\d{9}$/",$post['phone'])){
                $this->error('手机号格式不正确');
            }
            if(strlen($post['buildname']) > 22){
                $this->error('小区名称字数控制在10字内！');
            }
            if(strlen($post['des']) > 400){
                $this->error('详细描述字数控制在200字以内！');
            }
            /*获取用户的信息*/
            $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
            $send = $user['send']+1;
            $data = [
                'type'=>$post['type'],
                'build'=>$post['buildtype'],
                'buildname'=>$post['buildname'],
                'quyu'=>$post['quyu'],
                'des'=>$post['des'],
                'name'=>$post['name'],
                'yuezu'=>$post['yuezu'],
                'dizhi'=>$post['dizhi'],
                'phone'=>$post['phone'],
                'openid'=>$_SESSION['wechatopenid'],
                'mianji'=>$post['mianji'],
                'zhuangxiu'=>$post['zhuangxiu'],
                'huxing'=>$post['huxing'],
                'louceng'=>$post['louceng'],
                'fangling'=>$post['fangling'],
                'fukuan'=>'押'.$post['ya'].'付'.$post['fu'],
                'time'=>time()+3600*24*10,
                'read'=>1,
                'check'=>1,
                'sort'=>0,
                'fenyong'=>$post['fenyong'],
            ];
            if($user['send']==10){
                $this->error('您已超出当天限额10次，请改日发布。');
            }
            if(M('zhuanjie')->add($data)){
                M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->save(['send'=>$send,'sendtime'=>time()]);
                $this->success('发布成功!',U('Zhuan/index'));
            }else{
                $this->error('失败，未知错误！');
            }
        }else{
            /*获取用户信息*/
            $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
            /*当天0点时间*/
            $today = strtotime(date('Y-m-d', time()));

            if($today>$user['sendtime']){
                M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->save(['send'=>0]);
            }
            $this->display();
        }
    }
    /*新房*/
    public function xin()
    {
        /*微信自定义分享*/
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        (new WechatController())->zidingfenxiang($url);
        if(IS_POST){
            $post = i('post.');

            if(trim(empty($post['type']))) {
                $this->error('请选择售组类型');
            }
            if(trim(empty($post['buildtype']))) {
                $this->error('请选择售租种类');
            }

            if(trim(empty($post['buildname']))) {
                $this->error('请输入小区名称');
            }
            if(trim(empty($post['des']))) {
                $this->error('请输入详细描述');
            }
            if(trim(empty($post['yongjin']))) {
                $this->error('请输入佣金');
            }

            if(trim(empty($post['yuezu']))) {
                $this->error('请输入价格');
            }
            if(trim(empty($post['dizhi']))) {
                $this->error('请输入小区地址');
            }
            if(trim(empty($post['name']))) {
                $this->error('请输入发布人姓名');
            }
            if(trim(empty($post['phone']))) {
                $this->error('请输入发布人手机号');
            }
            if(trim(empty($post['jiesuan1']))) {
                $this->error('请输入结算时间');
            }
            if(!preg_match("/^1[34578]{1}\d{9}$/",$post['phone'])){
                $this->error('手机号格式不正确');
            }
            if(strlen($post['buildname']) > 22){
                $this->error('小区名称字数控制在10字内！');
            }
            if(strlen($post['des']) > 400){
                $this->error('详细描述字数控制在200字以内！');
            }
            /*获取用户的信息*/
            $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
            $send = $user['send']+1;
            $data = [
                'type'=>$post['type'],
                'build'=>$post['buildtype'],
                'buildname'=>$post['buildname'],
                'quyu'=>$post['quyu'],
                'des'=>$post['des'],
                'name'=>$post['name'],
                'yuezu'=>$post['yuezu'].$post['jiage'],
                'dizhi'=>$post['dizhi'],
                'phone'=>$post['phone'],
                'thumb'=>$post['thumb'],
                'yongjin'=>$post['yongjin'],
                'openid'=>$_SESSION['wechatopenid'],
                'time'=>time()+3600*24*10,
                'read'=>1,
                'check'=>1,
                'sort'=>0,
                'jiesuan'=>$post['jiesuan1'],
            ];

            if($user['send']==10){
                $this->error('您已超出当天限额10次，请改日发布。');
            }
            if(M('zhuanjie')->add($data)){
                M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->save(['send'=>$send,'sendtime'=>time()]);
                $this->success('发布成功!',U('Zhuan/index'));
            }else{
                $this->error('失败，未知错误！');
            }
        }else{
            /*获取用户信息*/
            $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
            /*当天0点时间*/
            $today = strtotime(date('Y-m-d', time()));

            if($today>$user['sendtime']){
                M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->save(['send'=>0]);
            }
            $this->display();
        }
    }
    /*转介出租*/
    public function chakan()
    {
        $zhuzhai = M('zhuanjie')->where(['type'=>1,'build'=>1])->order(['id'=>'desc','sort'=>'desc'])->select();
        $xiezilou = M('zhuanjie')->where(['type'=>1,'build'=>2])->order(['id'=>'desc','sort'=>'desc'])->select();
        $shangpu = M('zhuanjie')->where(['type'=>1,'build'=>3])->order(['id'=>'desc','sort'=>'desc'])->select();
        $cheku = M('zhuanjie')->where(['type'=>1,'build'=>4])->order(['id'=>'desc','sort'=>'desc'])->select();
        /*微信自定义分享*/
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        (new WechatController())->zidingfenxiang($url);
        $this->assign(['zhuzhai'=>$zhuzhai,'xiezilou'=>$xiezilou,'shangpu'=>$shangpu,'cheku'=>$cheku]);
        $this->display();
    }

    /*转介出售*/
    public function chushou()
    {
        $zhuzhai = M('zhuanjie')->where(['type'=>2,'build'=>1])->order(['id'=>'desc','sort'=>'desc'])->select();
        $xiezilou = M('zhuanjie')->where(['type'=>2,'build'=>2])->order(['id'=>'desc','sort'=>'desc'])->select();
        $shangpu = M('zhuanjie')->where(['type'=>2,'build'=>3])->order(['id'=>'desc','sort'=>'desc'])->select();
        $cheku = M('zhuanjie')->where(['type'=>2,'build'=>4])->order(['id'=>'desc','sort'=>'desc'])->select();
        /*微信自定义分享*/
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        (new WechatController())->zidingfenxiang($url);
        $this->assign(['zhuzhai'=>$zhuzhai,'xiezilou'=>$xiezilou,'shangpu'=>$shangpu,'cheku'=>$cheku]);
        $this->display();
    }
    /*转介新房*/
    public function xinfang()
    {
        $zhuzhai = M('zhuanjie')->where(['type'=>3,'build'=>1])->order(['id'=>'desc','sort'=>'desc'])->select();
        foreach ($zhuzhai as $k => $v){
            $zhuzhai[$k]['thumb'] = explode('|',$v['thumb']);
        }
        $xiezilou = M('zhuanjie')->where(['type'=>3,'build'=>2])->order(['id'=>'desc','sort'=>'desc'])->select();
        foreach ($xiezilou as $k => $v){
            $xiezilou[$k]['thumb'] = explode('|',$v['thumb']);
        }
        $shangpu = M('zhuanjie')->where(['type'=>3,'build'=>3])->order(['id'=>'desc','sort'=>'desc'])->select();
        foreach ($shangpu as $k => $v){
            $shangpu[$k]['thumb'] = explode('|',$v['thumb']);
        }
        $cheku = M('zhuanjie')->where(['type'=>3,'build'=>4])->order(['id'=>'desc','sort'=>'desc'])->select();
        foreach ($cheku as $k => $v){
            $cheku[$k]['thumb'] = explode('|',$v['thumb']);
        }
        /*
        echo '<pre/>';
        var_dump($zhuzhai);
        die();
        */
        /*微信自定义分享*/
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        (new WechatController())->zidingfenxiang($url);
        $this->assign(['zhuzhai'=>$zhuzhai,'xiezilou'=>$xiezilou,'shangpu'=>$shangpu,'cheku'=>$cheku]);
        $this->display();
    }
    /*出租住宅*/
    public function chuzuzz()
    {
        $id = i('get.id');

        $data = M('zhuanjie')->where(['id'=>$id])->find();
        $save = $data['read']+1;
        M('zhuanjie')->where(['id'=>$id])->save(['read'=>$save]);
        $this->assign('vo',$data);

        /*微信自定义分享*/
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        (new WechatController())->zidingfenxiang($url);

        $this->display();
    }
    /*出租写字楼*/
    public function chuzuxz()
    {
        $id = i('get.id');

        $data = M('zhuanjie')->where(['id'=>$id])->find();
        $save = $data['read']+1;
        M('zhuanjie')->where(['id'=>$id])->save(['read'=>$save]);
        $this->assign('vo',$data);

        /*微信自定义分享*/
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        (new WechatController())->zidingfenxiang($url);

        $this->display();
    }
    /*出租商铺*/
    public function chuzusp()
    {
        $id = i('get.id');

        $data = M('zhuanjie')->where(['id'=>$id])->find();
        $save = $data['read']+1;
        M('zhuanjie')->where(['id'=>$id])->save(['read'=>$save]);
        $this->assign('vo',$data);

        /*微信自定义分享*/
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        (new WechatController())->zidingfenxiang($url);

        $this->display();
    }
    /*出租车库*/
    public function chuzuck()
    {
        $id = i('get.id');

        $data = M('zhuanjie')->where(['id'=>$id])->find();
        $save = $data['read']+1;
        M('zhuanjie')->where(['id'=>$id])->save(['read'=>$save]);
        $this->assign('vo',$data);

        /*微信自定义分享*/
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        (new WechatController())->zidingfenxiang($url);

        $this->display();
    }
    /*出售住宅*/
    public function chushouzz()
    {
        $id = i('get.id');

        $data = M('zhuanjie')->where(['id'=>$id])->find();
        $save = $data['read']+1;
        M('zhuanjie')->where(['id'=>$id])->save(['read'=>$save]);
        $this->assign('vo',$data);

        /*微信自定义分享*/
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        (new WechatController())->zidingfenxiang($url);

        $this->display();
    }
    /*出售写字楼*/
    public function chushouxz()
    {
        $id = i('get.id');

        $data = M('zhuanjie')->where(['id'=>$id])->find();
        $save = $data['read']+1;
        M('zhuanjie')->where(['id'=>$id])->save(['read'=>$save]);
        $this->assign('vo',$data);

        /*微信自定义分享*/
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        (new WechatController())->zidingfenxiang($url);

        $this->display();
    }
    /*出售商铺*/
    public function chushousp()
    {
        $id = i('get.id');

        $data = M('zhuanjie')->where(['id'=>$id])->find();
        $save = $data['read']+1;
        M('zhuanjie')->where(['id'=>$id])->save(['read'=>$save]);
        $this->assign('vo',$data);

        /*微信自定义分享*/
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        (new WechatController())->zidingfenxiang($url);

        $this->display();
    }

    /*出售车库*/
    public function chushouck()
    {
        $id = i('get.id');

        $data = M('zhuanjie')->where(['id'=>$id])->find();
        $save = $data['read']+1;
        M('zhuanjie')->where(['id'=>$id])->save(['read'=>$save]);
        $this->assign('vo',$data);

        /*微信自定义分享*/
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        (new WechatController())->zidingfenxiang($url);

        $this->display();
    }
    /*修改*/
    public function ed()
    {
        echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
        $data = M('zhuanjie')->select();
        foreach ($data as $k){
            if($k['louceng']==''){
                M('zhuanjie')->where(['id'=>$k['id']])->save(['louceng'=>'未知']);
            }
            if($k['fangling']==''){
                M('zhuanjie')->where(['id'=>$k['id']])->save(['fangling'=>'未知']);
            }
        }
    }
    /*新房*/
    public function xinf()
    {
        $id = i('get.id');

        $data = M('zhuanjie')->where(['id'=>$id])->find();
        $save = $data['read']+1;
        M('zhuanjie')->where(['id'=>$id])->save(['read'=>$save]);
        $hello = explode('|',$data['thumb']);

        //array_pop($hello);
        /*
        echo '<pre/>';
        var_dump($hello);
        die();
        */
        $this->assign('lun',$hello);
        $this->assign('vo',$data);
        /*微信自定义分享*/
        $url = $_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
        (new WechatController())->zidingfenxiang($url);

        $this->display();
    }
    /*test*/
    public function test()
    {
        $this->display();
    }
}